package top.frsf.rpc.autoconfigure;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;
import top.frsf.rpc.autoconfigure.properties.RpcProperties;
import top.frsf.rpc.registry.ServiceRegistry;
import top.frsf.rpc.registry.impl.RedisServiceRegistry;
import top.frsf.rpc.registry.impl.ZookeeperServiceRegistry;

import static top.frsf.rpc.constants.RpcRegistryConstants.REDIS_REGISTRY;
import static top.frsf.rpc.constants.RpcRegistryConstants.ZK_REGISTRY;

/**
 * @author He Kuntao
 * @since 2025/7/11 15:33
 */
@Configuration
@RequiredArgsConstructor
@Import(RpcProperties.class)
public class RpcRegistryAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ServiceRegistry serviceRegistry(RpcProperties rpcProperties, StringRedisTemplate stringRedisTemplate) {
        RpcProperties.Registry registry = rpcProperties.getRegistry();
        if (REDIS_REGISTRY.equals(registry.getType())) {
            return new RedisServiceRegistry(stringRedisTemplate);
        } else if (ZK_REGISTRY.equals(registry.getType())) {
            return new ZookeeperServiceRegistry();
        } else {
            throw new RuntimeException("不支持的注册中心类型：" + registry.getType());
        }
    }
}
